/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.controller;

import com.tesam.gedeon.entity.Profile;
import com.tesam.gedeon.service.ProfileService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author joelpacheco
 */
@Controller
public class ProfileController {

    @Autowired
    private ProfileService profileService;

    @RequestMapping(value = "/profile/index")
    public ModelAndView index() {

        ModelAndView mv = new ModelAndView("/profile/index");

        return mv;
    }

    @RequestMapping(value = "/profile/new", method = RequestMethod.GET)
    public ModelAndView newProfile() {

//         Usuario usuario = new Usuario();
//        mav.getModelMap().put("usuario", usuario);
//        return mav;

        Profile profile = new Profile();
        ModelAndView mv = new ModelAndView("/profile/new");
        mv.addObject("profile", profile);
        return mv;
    }

    @RequestMapping(value = "/profile/save", method = RequestMethod.POST)
    public ModelAndView saveProfile(@ModelAttribute("profile") Profile profile,
            SessionStatus status,
            HttpServletRequest request) {

        ModelAndView mv = new ModelAndView("/profile/save");
        profileService.save(profile);
        mv.addObject("mensaje", "Grabo");
        return mv;
    }

    @RequestMapping(value = "/profile/ajax", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxGet() {

        int cantidadRegistros = 0;
        try {
            List<Profile> list = profileService.getAllByLimit(10, 10, "id", "ASC");

            // ArrayList<Cliente> list = clienteService.getAllClientes();
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();

            if (list.size() > 0) {
                for (Profile profile : list) {
                    JSONArray arrayPut = new JSONArray();
                    arrayPut.put(profile.getId());
                    arrayPut.put(profile.getName());
                    arrayPut.put(profile.getDescription());
                    array.put(arrayPut);
                    // cantidadRegistros++;
                }
            }

            cantidadRegistros = profileService.getCountTotal();
            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.print("Error - Contprofileler-doAjax:" + e.getMessage());
        }

        return null;
    }

    @RequestMapping(value = "/profile/ajax", method = RequestMethod.POST)
    public @ResponseBody
    String doAjaxPost(HttpServletRequest request) {

        List<Profile> listaDeProfile = null;
        int cantidadRegistros = 0;

        String[] columns = {"id", "name", "description", "datecreated"};

        String startLimit = request.getParameter("iDisplayStart");
        String endLimit = request.getParameter("iDisplayLength");
        String dataToSort = columns[Integer.valueOf(request
                .getParameter("iSortCol_0"))];
        String directionOfOrder = request.getParameter("sSortDir_0");
        String textSearch = request.getParameter("sSearch").toUpperCase();

        int _startLimit = 0;
        int _endLimit = 10;

        if (startLimit != null && !startLimit.equals("0")) {

            if (!startLimit.trim().isEmpty()) {
                _startLimit = Integer.valueOf(startLimit);
            }
        }

        if (endLimit != null && !endLimit.equals("0")) {
            if (!endLimit.trim().isEmpty()) {
                _endLimit = Integer.valueOf(endLimit);
            }
        }

        if (textSearch != null) {
            if (!textSearch.trim().isEmpty()) {
                textSearch = textSearch.trim();
                Profile profile = new Profile();
                profile.setName(textSearch);
                listaDeProfile = profileService.searchByName(profile, _startLimit,
                        (_startLimit + _endLimit));
                cantidadRegistros = profileService.getCountSearchByName(profile);
                return profileService.getJsonDataFromArrayList(listaDeProfile, cantidadRegistros);

            }
        }

        listaDeProfile = profileService.getAllByLimit(_startLimit,
                (_startLimit + _endLimit), dataToSort, directionOfOrder);

        cantidadRegistros = profileService.getCountTotal();

        return profileService.getJsonDataFromArrayList(listaDeProfile, cantidadRegistros);

    }
}
